{extend name="public/base"}

{block name="css"}
<style>
    .layui-upload-img {
        width: 92px;
        height: 92px;
        margin: 0 10px 10px 0;
    }
</style>
{/block}

{block name="main"}
<div class="layui-card">
    <blockquote class="layui-elem-quote layui-quote-nm">{empty name="data"}添加{else /}编辑{/empty}管理员</blockquote>
    <form class="layui-form" action="" lay-filter="component-form-group">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
            <legend>基本信息</legend>
        </fieldset>
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-inline">
                <input type="text" lay-verify="required" placeholder="请输入"
                       autocomplete="off"
                       class="layui-input"  name="username" value="{$data.username|default=''}" {notempty name="data"}readonly{/notempty}>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-inline">
                <input type="password" name="password" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">确认密码</label>
            <div class="layui-input-inline">
                <input type="password" name="password_confirm" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-inline">
                <input type="text" lay-verify="required" placeholder="请输入"
                       autocomplete="off"
                       class="layui-input" name="nickname" value="{$data.nickname|default=''}">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">选择角色</label>
                {eq name="data.id|default=0" value="1"}
                    <div class="layui-form-mid layui-word-aux">超级管理员</div>
                    <input name="role_ids" type="hidden" value="{$data.role_ids}" />
                {else /}
                    <div class="layui-input-block" id="role">
                    </div>
                {/eq}
            </div>
        </div>

        <div class="layui-form-item layui-layout-admin">
            <div class="layui-input-block">
                <div class="layui-footer" style="left: 0;">
                    <input type="hidden" name="id" value="{$data.id|default=0}">
                    <button class="layui-btn" lay-submit="" lay-filter="component-form-demo1">立即提交</button>
                </div>
            </div>
        </div>

    </form>
</div>
{/block}

{block name="js"}
<script>
    layui.config({
        base: '/layui_admin/src/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form', 'laydate', 'upload', 'selectM'], function () {
        let $ = layui.$
            , form = layui.form, admin = layui.admin, laydate = layui.laydate, layer = layui.layer, upload = layui.upload, selectM = layui.selectM;

        let  tagData = '';
        admin.req({
            url: '{:url("get_tag_data")}',
            data : {
                id : '{$data.id|default=0}'
            },
            done : function (res) {
                let selected = [];
                $.each(res.selected, function (i, d) {
                    selected.push(d);
                });
                tagData = res.data;
                //多选标签-所有配置
                selectM({
                    //元素容器【必填】
                    elem: '#role'
                    //候选数据【必填】
                    ,data: tagData
                    //最多选中个数，默认5
                    ,max : 10
                    //默认值
                    ,selected: selected
                    //input的name 不设置与选择器相同(去#.)
                    ,name: 'role_ids'
                    //候选项数据的键名
                    ,field: {idName:'id',titleName:'name'}
                });
            }
        });

        /* 监听提交 */
        form.on('submit(component-form-demo1)', function (data) {
            layer.load();
            //请求登入接口
            admin.req({
                url: "{:url('save_admin')}" //实际使用请改成服务端真实接口
                ,type : 'post'
                ,data: data.field
                ,done: function(res){
                    //登入成功的提示与跳转
                    layer.msg('保存成功', {
                        offset: '15px'
                        ,icon: 1
                        ,time: 1000
                    }, function(){
                        parent.location.reload(); //刷新页面
                    });
                }
            });
            return false;
        });
    });
</script>
{/block}